package com.jhy.new2025.BinaryTree;

import java.util.ArrayDeque;

public class MinDepth {
    public int minDepth(TreeNode root) {
        int count = 0;
        if (root == null) return count;
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            while (size > 0) {
                TreeNode node = deque.poll();
                if (node.left == null && node.right == null) {
                    return count;
                }
                if (node.left != null) deque.offer(node.left);
                if (node.right != null) deque.offer(node.right);
                size--;
            }
            count++;
        }
        return count;

    }
}
